package com.chat.datea.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.chat.datea.R;
import com.chat.datea.adapter.SongListViewAdapter;
import com.chat.datea.bean.SearchResultEntity;
import com.chat.datea.utils.volley.VolleySingleton;
import com.google.gson.Gson;

import java.util.List;

/**
 * Created by dllo on 15/10/23.
 */
public class SearchResultMidiFragment extends Fragment {

    private String url = "http://tingapi.ting.baidu.com/v1/restserver/ting?from=android&version=5.6.5.2&method=baidu.ting.search.merge&format=json&query=%E5%8D%83%E6%9C%AC%E6%A8%B1-%E5%88%9D%E9%9F%B3%E6%9C%AA%E6%9D%A5&page_no=1&page_size=50&type=-1&data_source=0&use_cluster=1";
    private List<SearchResultEntity.ResultEntity.SongInfoEntity.SongListEntity> songListEntities;
    private ListView song_listView;
    private VolleySingleton volleySingleton;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.song_listview, null);
        initView(view);
        getData();
        return view;
    }

    private void initView(View view) {
        song_listView = (ListView) view.findViewById(R.id.song_recyclerView);
        volleySingleton = VolleySingleton.getVolleySingleton(getContext());
    }

    private void getData() {

        StringRequest request = new StringRequest(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Gson gson = new Gson();
                SearchResultEntity searchResultEntity = gson.fromJson(response, SearchResultEntity.class);
                songListEntities = searchResultEntity.getResult().getSong_info().getSong_list();
                Log.i("chouru", "-----" + songListEntities);
                SongListViewAdapter songListViewAdapter = new SongListViewAdapter(getActivity(), songListEntities);
                song_listView.setAdapter(songListViewAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(request, "tag");
    }
}
